package com.annoncesautomobile.modele.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

public class DrawableDowloader extends AsyncTask<String, Integer, Drawable>{
	
	private Resources res;

	public Resources getRes() {
		return res;
	}

	public void setRes(Resources res) {
		this.res = res;
	}

	@Override
	protected Drawable doInBackground(String... params) {
		InputStream is = null;
		HttpGet httpGet = new HttpGet(params[0]);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse response;
		try {
			response = httpClient.execute(httpGet);
			is = response.getEntity().getContent();
			return Drawable.createFromStream(is, null);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		

		
		return null;
	}
	
	public static Drawable getDrawable(String urlString)
	{
		DrawableDowloader drawableDowloader = new DrawableDowloader();
		try {
			return drawableDowloader.execute(urlString).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		return null;
	}
}